<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单code" prop="formSettingBigId">
        <el-input
          v-model="queryParams.formSettingBigId"
          placeholder="请输入表单code"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小项描述" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入小项描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="控件类型" prop="component">
        <el-input
          v-model="queryParams.component"
          placeholder="请输入控件类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序;多选，输入等组件类型" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序;多选，输入等组件类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认;1是默认 0否" prop="isSelect">
        <el-input
          v-model="queryParams.isSelect"
          placeholder="请输入是否默认;1是默认 0否"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认文本;如果是输入框等就有文本" prop="defaultText">
        <el-input
          v-model="queryParams.defaultText"
          placeholder="请输入默认文本;如果是输入框等就有文本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小值" prop="valueMin">
        <el-input
          v-model="queryParams.valueMin"
          placeholder="请输入最小值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大值" prop="valueMax">
        <el-input
          v-model="queryParams.valueMax"
          placeholder="请输入最大值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小项描述注释" prop="labelRemark">
        <el-input
          v-model="queryParams.labelRemark"
          placeholder="请输入小项描述注释"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:formsettingsmall:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:formsettingsmall:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:formsettingsmall:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:formsettingsmall:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formsettingsmallList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="formSettingSmallId" />
      <el-table-column label="表单code" align="center" prop="formSettingBigId" />
      <el-table-column label="小项描述" align="center" prop="label" />
      <el-table-column label="控件类型" align="center" prop="component" />
      <el-table-column label="排序;多选，输入等组件类型" align="center" prop="sort" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="是否默认;1是默认 0否" align="center" prop="isSelect" />
      <el-table-column label="默认文本;如果是输入框等就有文本" align="center" prop="defaultText" />
      <el-table-column label="最小值" align="center" prop="valueMin" />
      <el-table-column label="最大值" align="center" prop="valueMax" />
      <el-table-column label="小项描述注释" align="center" prop="labelRemark" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:formsettingsmall:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:formsettingsmall:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单小项对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formsettingsmallRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单code" prop="formSettingBigId">
          <el-input v-model="form.formSettingBigId" placeholder="请输入表单code" />
        </el-form-item>
        <el-form-item label="小项描述" prop="label">
          <el-input v-model="form.label" placeholder="请输入小项描述" />
        </el-form-item>
        <el-form-item label="控件类型" prop="component">
          <el-input v-model="form.component" placeholder="请输入控件类型" />
        </el-form-item>
        <el-form-item label="排序;多选，输入等组件类型" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序;多选，输入等组件类型" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="是否默认;1是默认 0否" prop="isSelect">
          <el-input v-model="form.isSelect" placeholder="请输入是否默认;1是默认 0否" />
        </el-form-item>
        <el-form-item label="默认文本;如果是输入框等就有文本" prop="defaultText">
          <el-input v-model="form.defaultText" placeholder="请输入默认文本;如果是输入框等就有文本" />
        </el-form-item>
        <el-form-item label="最小值" prop="valueMin">
          <el-input v-model="form.valueMin" placeholder="请输入最小值" />
        </el-form-item>
        <el-form-item label="最大值" prop="valueMax">
          <el-input v-model="form.valueMax" placeholder="请输入最大值" />
        </el-form-item>
        <el-form-item label="小项描述注释" prop="labelRemark">
          <el-input v-model="form.labelRemark" placeholder="请输入小项描述注释" />
        </el-form-item>
        <el-form-item label="0正常1删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0正常1删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Formsettingsmall">
import { listFormsettingsmall, getFormsettingsmall, delFormsettingsmall, addFormsettingsmall, updateFormsettingsmall } from "@/api/manage/formsettingsmall";

const { proxy } = getCurrentInstance();

const formsettingsmallList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    formSettingBigId: null,
    label: null,
    component: null,
    sort: null,
    unit: null,
    isSelect: null,
    defaultText: null,
    valueMin: null,
    valueMax: null,
    labelRemark: null,
    status: null,
  },
  rules: {
    formSettingSmallId: [
      { required: true, message: "id不能为空", trigger: "blur" }
    ],
    formSettingBigId: [
      { required: true, message: "表单code不能为空", trigger: "blur" }
    ],
    label: [
      { required: true, message: "小项描述不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    delFlag: [
      { required: true, message: "0正常1删除不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询单小项列表 */
function getList() {
  loading.value = true;
  listFormsettingsmall(queryParams.value).then(response => {
    formsettingsmallList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    formSettingSmallId: null,
    formSettingBigId: null,
    label: null,
    component: null,
    sort: null,
    unit: null,
    isSelect: null,
    defaultText: null,
    valueMin: null,
    valueMax: null,
    labelRemark: null,
    status: null,
    delFlag: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("formsettingsmallRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.formSettingSmallId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加单小项";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _formSettingSmallId = row.formSettingSmallId || ids.value
  getFormsettingsmall(_formSettingSmallId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改单小项";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formsettingsmallRef"].validate(valid => {
    if (valid) {
      if (form.value.formSettingSmallId != null) {
        updateFormsettingsmall(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFormsettingsmall(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _formSettingSmallIds = row.formSettingSmallId || ids.value;
  proxy.$modal.confirm('是否确认删除单小项编号为"' + _formSettingSmallIds + '"的数据项？').then(function() {
    return delFormsettingsmall(_formSettingSmallIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/formsettingsmall/export', {
    ...queryParams.value
  }, `formsettingsmall_${new Date().getTime()}.xlsx`)
}

getList();
</script>
